class Employee {
    String employeeId//
    String firstName//
    String lastName//
    Date dob//
    String title//
    Double salary//
    String email//
    String street//
    String phoneNumber
    String city
    Integer zipCode
    Area hiredBy
    Integer mode = 1 //default mode is sandbox mode and 0 is budget mode

    //attributes for applicants
    String bankAccount = null //
    String faculty = null //
    String department = null
    String division = null
    Boolean isApplicant =false //
    String billingAdress = null //
    String lukasId = null //
    
   // Boolean admin//
    Float percentageWork = 100.0//

    static hasMany = [loaner : Bwhoa, worksIn : Whoa]

    static Integer workingHoursBellow = 1732
    static Integer workingHoursOver = 1700

    static Integer fortyYears = 40

    static transients = ['workingHoursPerYear']
    static belongsTo = Area
    
    static constraints = {
        //personal informations
        firstName(size:2..20,matches:"[a-öA-Ö ]+",blank:false)
        lastName(size:2..20,matches:"[a-öA-Ö ]+",blank:false)
        street(size:2..20,matches:"[a-öA-Ö0-9 ]+",blank:false)
        city(size:2..20,matches:"[a-öA-Ö ]+",blank:false)
        zipCode(min:10000,max:99999,blank:false)
        dob(max:new Date())
        email(size:5..50,email:true,blank:false)
        hiredBy(nullable:true)
        //professional informations
        title(size:1..20,blank:false)
        salary(min:0d,max:999999d,blank:false) // cannot have matches on double only on string
        percentageWork(min:0f,max:100f)
        employeeId(unique:true,blank:false,matches:"[0-9 ]+")


        bankAccount(nullable:true)
        faculty(nullable:true)
        phoneNumber(nullable:true, matches:"[0-9 ]+")
        billingAdress(nullable:true,matches:"[a-öA-Ö0-9 ]+")
        lukasId(nullable:true,matches:"[a-öA-Ö0-9 ]+")
        department(nullable:true,matches:"[a-öA-Ö0-9 ]+")
        division(nullable:true,matches:"[a-öA-Ö0-9 ]+")
    }
    //Interger salaryPerYear() {
    //    return actualWorkingPercentagePerYear()*
    //}
   
    Boolean isWorkingOverTime(){
        if (this.actualWorkingHoursPerYear() > this.getWorkingHoursPerYear()) {
            return true
        }
        return false
    }

    Boolean isWorkingLowerTime(){
        if (this.actualWorkingPercentagePerYear() < this.percentageWork) {
            return true
        }
        return false
    }

    /// Return hours that employee should work per year ///
    Float hoursWork(){
        return (percentageWork/100)*this.getWorkingHoursPerYear()
    }

    Float percentageNotAssigned() {
        println("tot"+ (100*((this.getWorkingHoursPerYear()-this.actualWorkingHoursPerYear())/this.hoursForAge())))
        println("getWorkingHoursPerYear: "+ (this.getWorkingHoursPerYear()))
        println("actualWorkingHoursPerYear: "+ (this.actualWorkingHoursPerYear()))
        println("hoursForAge: "+(this.hoursForAge()))
        return 100*((this.getWorkingHoursPerYear()-this.actualWorkingHoursPerYear())/this.hoursForAge())
    }

    Float hoursNotAssigned() {
        return (this.getWorkingHoursPerYear()-this.actualWorkingHoursPerYear())
    }





    Float getWorkingHoursPerYear() {
        Date today = new Date();
        // when create new date, date.getYear returns the current year minus 1900. Crazy Java !
        if (dob != null && ((today.getYear()+1900) - dob.getYear()) >= fortyYears) {
            return percentageWork * workingHoursOver / 100
        } else {
            return percentageWork * workingHoursBellow / 100
        }
    }

    Integer hoursForAge() {
        Date today = new Date();
        // when create new date, date.getYear returns the current year minus 1900. Crazy Java !
        if (dob != null && ((today.getYear()+1900) - dob.getYear()) >= fortyYears) {
            return workingHoursOver
        } else {
            return workingHoursBellow
        }
    }

    public List listOfProjects(){ // "List" = What will be returned
        Date d = new Date();
        ArrayList l =  new ArrayList();
        if(DBConnection.isConnectionStarted()){
            for(Whoa a in DBConnection.getWorksIn(this)){         //for every whoa in the whoalist(?)
                if(a?.year == d.getYear()){ //if the year for the project that the employee works in is equal to this year (2009) add to the list l
                    l.add(a);
                }
            }
        }
        else{
            for(Whoa a in worksIn){         //for every whoa in the whoalist(?)
                if(a?.year == d.getYear()){ //if the year for the project that the employee works in is equal to this year (2009) add to the list l
                    l.add(a);
                }
            }
        }

        //println(l)  //want to see this!
        return l;
    }

    Float actualWorkingHoursPerYear() {      //calculate the total ammout of worked hours for one employee in all project for one year
        List l = this.listOfProjects()         //call the function in this class to get the list with whoa of this year
        int sum = 0
        for (Whoa i in l) {
            sum = i.hours+sum
        }
        return sum
    }

    Boolean equals(Employee e){
        return this.id==e.id
    }

    Float actualWorkingPercentagePerYear() {
        return 100*(this.actualWorkingHoursPerYear()/this.hoursForAge())
    }

    Float updateWorkPercentageWithHours(Float newFigure) {
     return 100*(newFigure/hoursForAge())
     }


   
////////////////////////ARROW SECTION////////////////////////////////////

    Float arrowUp() {
        percentageWork = percentageWork+1
        return percentageWork
    }
    public Container container(){
       // return this.hiredBy?.container;
       Container t = new Container(name:"bla");
    }

    Float arrowDown () {
        percentageWork = percentageWork-1
        return percentageWork
    }



     String toString(){"${this.firstName} ${this.lastName} "}


}
